package com.kapark.cloud.context;

import com.ddclient.dongsdk.DongSDKProxy;
import com.kapark.cloud.config.FluteConfig;
import com.kapark.cloud.socket.AudioSocket;
import com.kapark.cloud.utils.AMAudioFormat;
import org.springframework.stereotype.Component;

import javax.sound.sampled.*;
import java.io.*;

/**
 * @author: hyzhan
 * @date: 2019/6/5
 * @desc: TODO
 */
@Component
public class AudioSender {

    private static AudioFormat gsmFormat;
    private static AudioFormat pcmFormat;

    public AudioSender() {
        // gsm 格式
        gsmFormat = AMAudioFormat.getNetAudioFormat(AMAudioFormat.FORMAT_CODE_GSM);

        // PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
        pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                FluteConfig.SAMPLE_RATE,
                FluteConfig.SAMPLE_SIZE_IN_BITS,
                FluteConfig.CHANNEL,
                FluteConfig.FRAME_SIZE,
                FluteConfig.FRAME_RATE,
                false);
    }

    /**
     * 根据 设备id号 发送音频给 对讲机
     */
    public static void send2Intercom(int devId, byte[] data) {
        try {

            // 把 传过来的音频数据转化成 inputStream
            InputStream inputStream = new ByteArrayInputStream(data);

            // 在根据 inputStream 转化成 audioInputStream (音频输入数据流)
            AudioInputStream pcmInputStream = AudioSystem.getAudioInputStream(inputStream);
            // 转码, pcm数据类型 转化成 gsm 类型
            AudioInputStream gsmInputStream = AudioSystem.getAudioInputStream(gsmFormat, pcmInputStream);

            // 这个byte大小可根据自身自行调整
            byte[] tempBytes = new byte[50];
            int len;
            while ((len = gsmInputStream.read(tempBytes)) != -1) {
                // 调用对讲机相关方法(requestSendAudioData), 发送给对讲机
                DongSDKProxy.requestSendAudioData(devId, tempBytes, len);
            }
        } catch (UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }


    public void send2Client(int devId, byte[] data, long total) {


        /**
         * Unsupported conversion: PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian from
         * GSM0610 8000.0 Hz, unknown bits per sample, mono, 33 bytes/frame, 50.0 frames/second,
         *
         * *.wav
         * wav format : WAVE (.wav) file, byte length: 8286658,
         * data format: PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian, frame length: 4143238
         *
         */
        try (InputStream inputStream = new ByteArrayInputStream(data);
             AudioInputStream gsmInputStream = AudioSystem.getAudioInputStream(inputStream);
             AudioInputStream pcmInputStream = AudioSystem.getAudioInputStream(pcmFormat, gsmInputStream)) {

            byte[] tempBytes = new byte[50];
            int len;
            while ((len = pcmInputStream.read(tempBytes)) != -1) {
                AudioSocket.send2Client(devId, tempBytes, len);
                //Thread.sleep(15);
            }
        } catch (UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }
}
